import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class HoleBild extends JPanel {
	private JButton ladeBild;
	private JLabel bild;

	public HoleBild(final JFrame frame) {
		setLayout(new BorderLayout());
		ladeBild = new JButton("lade bild...");
		add(ladeBild, BorderLayout.NORTH);
		ladeBild.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					JFileChooser fc = new JFileChooser();
					if (fc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
						File selectedFile = fc.getSelectedFile();
						URL resource = selectedFile.toURI().toURL();
						ImageIcon imageIcon = new ImageIcon(resource);
						bild.setIcon(imageIcon);
						frame.pack();
					}
				} catch (HeadlessException e1) {
					e1.printStackTrace();
				} catch (MalformedURLException e1) {
					e1.printStackTrace();
				}
			}
		});
		bild = new JLabel();
		add(bild, BorderLayout.CENTER);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("Bild");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(new HoleBild(frame));
		frame.pack();
		frame.setVisible(true);
	}
}
